package exia.java.core.services;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

import exia.java.core.entities.Commande;
import exia.java.core.entities.Livre;

class HibernateServiceLivreImpl implements IServiceLivre {


	private static final String REMOTE_SERVICE_NAME = "Hibernate Remote Livre Service";
	private static final double REMOTE_SERVICE_VERSION = 0.1;
	private Session WORKING_SESSION;
	/**
	 * 
	 * @param workingSession
	 */
	public HibernateServiceLivreImpl(final Session workingSession){
		super();
		WORKING_SESSION = workingSession;
	}
	
	/**
	 * 
	 */
	public HibernateServiceLivreImpl(){
		super();
	}
	
	/**
	 * 
	 * @param workingSession
	 */
	public void changeWorkingSession(final Session workingSession){
		if(workingSession != null){
			this.WORKING_SESSION = workingSession;
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public Session getWorkingSession(){
		return this.WORKING_SESSION;
	}
	
	
	public boolean addLivre(final Livre livre) throws ServiceLivreException {
		boolean isAdded = false;
		Transaction tx = null;
		if(this.WORKING_SESSION != null){
			if(livre != null){
				try {
					tx = WORKING_SESSION.beginTransaction();
					WORKING_SESSION.save(livre);
					tx.commit();
					if(tx.wasCommitted()){
						isAdded = true;
					}
				} catch (HibernateException ex) {
					ex.printStackTrace();
					throw new ServiceLivreException(ex);
				}
			}else{
				throw new ServiceLivreException("\nNULL SESSION ERROR: Unable to add the desired Livre.\n\tcause: the Livre parameter of the addLivre method is null.\n");
			}
		}else{
			throw new ServiceLivreException("\nNULL SESSION ERROR: Unable to add the desired Livre.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return isAdded;
	}

	public int countLivres() throws ServiceLivreException {
		int nbLivres = 0;
		if(this.WORKING_SESSION != null){
			List<Livre> tmp = this.getLivres();
			if((tmp != null)&&(tmp.size() > 0)){
				nbLivres = tmp.size();
			}
		}else{
			throw new ServiceLivreException("\nNULL SESSION ERROR: Unable to acount the number of Livres.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return nbLivres;
	}

	public Livre getLivreById(final int id) throws ServiceLivreException {
		Livre livre = null;
		List<Livre> livres = null;
		if(this.WORKING_SESSION != null){
			if(id > 0){
				livres = this.getLivres();
				if((livres != null)&&(livres.size() > 0)){
					for(Livre l : livres){
						if(l.getId() == id){
							livre = l;
						}
					}
				}
			}else{
				throw new ServiceLivreException("\nID VALUE ERROR: The id parameter of the getLivreById must be greater than 0.\n");
			}
		}else{
			throw new ServiceLivreException("\nNULL SESSION ERROR: Unable to get the desired Livres.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return livre;
	}

	public Livre getLivreByTitre(final String titre) throws ServiceLivreException {
		Livre livre = null;
		List<Livre> tmp = null;
		if(this.WORKING_SESSION != null){
			if(titre != null){
				if(titre.length() > 0){
					tmp = this.getLivres();
					if((tmp != null)&&(tmp.size() > 0)){
						for(Livre l : tmp){
							if(l.getTitre().equalsIgnoreCase(titre)){
								livre = l;
							}
						}
					}
				}
			}else{
				throw new ServiceLivreException("\nNULL POINTER ERROR: The titre parameter of the getLivreByTitre is null.\n");
			}
		}else{
			throw new ServiceLivreException("\nNULL SESSION ERROR: Unable to get the Livre with the desired id.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return livre;
	}

	public List<Livre> getLivres() throws ServiceLivreException {
		List<Livre> livres = null;
		Iterator it = null;
		Livre c = null;
		Query query = null;
		if(this.WORKING_SESSION != null){
			try {
				livres = new ArrayList<Livre>();
				String SQL_QUERY ="from Livre livre";
					 query = this.WORKING_SESSION.createQuery(SQL_QUERY);
					 if(query != null){
						 for(it=query.iterate();it.hasNext();){
					     	c = (Livre) it.next();
					     	livres.add(c);
					     }
					 }
			} catch (HibernateException ex) {
				ex.printStackTrace();
				livres = null;
				throw new ServiceLivreException(ex);
			}
		}else{
			throw new ServiceLivreException("\nNULL SESSION ERROR: Unable to get the Livres.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return livres;
	}

	public List<Livre> getLivresByAuteur(final String auteur) throws ServiceLivreException {
		List<Livre> livres = null;
		List<Livre> tmp = null;
		if(this.WORKING_SESSION != null){
			if(auteur != null){
				tmp = this.getLivres();
				if((tmp != null)&&(tmp.size() > 0)){
					for(Livre l : tmp){
						if(l.getAuteur().equalsIgnoreCase(auteur)){
							if(livres == null){
								livres = new ArrayList<Livre>();
							}
							livres.add(l);
						}
					}
				}
			}else{
				throw new ServiceLivreException("\nNULL POINTER ERROR: The auteur parameter of the getLivresByAuteur method is null.\n");
			}
		}else{
			throw new ServiceLivreException("\nNULL SESSION ERROR: Unable to get the Livres with the desired auteur.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return livres;
	}

	public boolean hasLivre(final Livre livre) throws ServiceLivreException {
		boolean has = false;
		List<Livre> tmp = null;
		if(this.WORKING_SESSION != null){
			if(livre != null){
				tmp = this.getLivres();
				if((tmp != null)&&(tmp.size() > 0)){
					for(Livre l : tmp){
						if(l.equals(livre)){
							has = true;
							break;
						}
					}
				}
			}else{
				throw new ServiceLivreException("\nNULL POINTER ERROR: The Livre parameter of the hasLivre method is null.\n");
			}
		}else{
			throw new ServiceLivreException("\nNULL SESSION ERROR: Unable to check if the desired Livre exists.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return has;
	}

	public boolean hasLivreWithTitre(final String titre) throws ServiceLivreException {
		boolean has = false;
		List<Livre> tmp = null;
		if(this.WORKING_SESSION != null){
			if(titre != null){
				tmp = this.getLivres();
				if((tmp != null)&&(tmp.size() > 0)){
					for(Livre l : tmp){
						if(l.getTitre().equalsIgnoreCase(titre)){
							has = true;
							break;
						}
					}
				}
			}else{
				throw new ServiceLivreException("\nNULL POINTER ERROR: The titre parameter of the hasLivreWithTitre method is null.\n");
			}
		}else{
			throw new ServiceLivreException("\nNULL SESSION ERROR: Unable to check if the Livre with the desired titre exists.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return has;
	}

	public boolean hasLivres() throws ServiceLivreException {
		boolean has = false;
		if(this.WORKING_SESSION != null){
			List<Livre> tmp = this.getLivres();
			if(tmp != null && tmp.size() > 0){
				has = true;
			}
		}else{
			throw new ServiceLivreException("\nNULL SESSION ERROR: Unable check Livres.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return has;
	}

	public boolean hasLivresWithAuteur(final String auteur) throws ServiceLivreException {
		boolean has = false;
		List<Livre> tmp = null;
		if(this.WORKING_SESSION != null){
			if(auteur != null){
				tmp = this.getLivres();
				if((tmp != null)&&(tmp.size() > 0)){
					for(Livre l : tmp){
						if(l.getAuteur().equalsIgnoreCase(auteur)){
							has = true;
							break;
						}
					}
				}
			}else{
				throw new ServiceLivreException("\nNULL POINTER ERROR: The auteur parameter of the hasLivresWithAuteur method is null.\n");
			}
		}else{
			throw new ServiceLivreException("\nNULL SESSION ERROR: Unable to check if the Livre with the desired auteur exists.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return has;
	}

	public boolean removeLivre(final Livre livre) throws ServiceLivreException {
		boolean isRemoved = false;
		Transaction tx = null;
		if(this.WORKING_SESSION != null){
			if((livre != null)){
				try {
					tx = this.WORKING_SESSION.beginTransaction();
					this.WORKING_SESSION.delete(livre);
					tx.commit();
					if(tx.wasCommitted()){
						isRemoved = true;
					}
				} catch (HibernateException ex) {
					ex.printStackTrace();
					throw new ServiceLivreException(ex);
				}
			}else{
				throw new ServiceLivreException("\nNULL POINTER ERROR: Commande parameter of the removeLivre method is null.\n");
			}
		}else{
			throw new ServiceLivreException("\nNULL SESSION ERROR: Unable to remove the desired Livre.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return isRemoved;
	}

	public boolean updateLivre(final Livre livre) throws ServiceLivreException {
		boolean isUpdated = false;
		Transaction tx = null;
		if(this.WORKING_SESSION != null){
			if((livre != null)){
				try {
					tx = this.WORKING_SESSION.beginTransaction();
					this.WORKING_SESSION.update(livre);
					tx.commit();
					if(tx.wasCommitted()){
						isUpdated = true;
					}
				} catch (HibernateException ex) {
					ex.printStackTrace();
					throw new ServiceLivreException(ex);
				}
			}else{
				throw new ServiceLivreException("\nNULL POINTER ERROR: Commande parameter of the removeCommande method is null.\n");
			}
		}else{
			throw new ServiceLivreException("\nNULL SESSION ERROR: Unable to update the desired Commande.\n\tcause: the current WORKING_SESSION field of the "+REMOTE_SERVICE_NAME+" (version "+REMOTE_SERVICE_VERSION+") is null.\n");
		}
		return isUpdated;
	}

	public String getRemoteServiceName() {
		return REMOTE_SERVICE_NAME;
	}

	public double getRemoveServiceVersion() {
		return REMOTE_SERVICE_VERSION;
	}

	public Object performRemoteServiceFunction(final String functionName, Object[] params) throws RemoteServiceException {
		// TODO Auto-generated method stub
		return null;
	}

}
